package cn.wdm.system.service.impl;

import cn.wdm.system.domain.Role;
import cn.wdm.system.mapper.RoleMapper;
import cn.wdm.system.service.IRoleService;
import cn.wdm.basic.service.impl.BaseServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author laozhang
 * @since 2022-06-21
 */
@Service
public class RoleServiceImpl extends BaseServiceImpl<Role> implements IRoleService {
    @Autowired
    private RoleMapper roleMapper;
    /**
     * 重写保存角色的方法，
     * 1.因为保存角色要给角色分配按钮权限和按钮权限
     * 2.级联操作role-permission和role-menu表
     * 3操作中间表要先删除之前的中间表数据才保存
     * @param role
     */
    @Override
    public void save(Role role) {
        roleMapper.save(role);
        //初始化关系表 role-permission和role-menu表
        initRelationTable(role);
    }

    private void initRelationTable(Role role) {
        //根据roleid删除两张关系表的数据
        roleMapper.deleteRolePermissionByRoleId(role.getId());
        roleMapper.deleteRoleMenuByRoleId(role.getId());
       //然后通过前端传的关系表中的ids增加关系表
        roleMapper.saveRolePermissionByIds(role);
        roleMapper.saveRoleMenuByIds(role);
    }
}
